package com.whfc.openapi.api.fse.controller;

import com.whfc.common.result.ListData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.fse.dto.OpenApiFseCraneDTO;
import com.whfc.fse.dto.OpenApiFseCraneDataDTO;
import com.whfc.fse.dto.OpenApiFseCraneRecordDTO;
import com.whfc.fse.param.FseCraneDataAddParam;
import com.whfc.fuum.dto.open.OpenApiClientDTO;
import com.whfc.openapi.api.fse.service.OpenApiFseCraneService;
import com.whfc.openapi.constant.OpenApiConst;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @ClasssName FseCraneController
 * @Description 塔机开放接口
 * @Author hw
 * @Date 2021/2/4 16:04
 * @Version 1.0
 */
@RestController
@RequestMapping("/open/api/v1/fse/crane")
public class FseCraneController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private OpenApiFseCraneService fseCraneService;


    @GetMapping("/list")
    public Result list(@RequestAttribute(OpenApiConst.OPENAPI_CLIENT) OpenApiClientDTO openApiClientDTO) {
        Integer deptId = openApiClientDTO.getDeptId();
        logger.info("开放平台获取塔机列表,deptId:{}", deptId);
        ListData<OpenApiFseCraneDTO> pageData = fseCraneService.list(deptId);
        return ResultUtil.success(pageData);
    }

    @GetMapping("/device/info")
    public Result deviceInfo(@RequestAttribute(OpenApiConst.OPENAPI_CLIENT) OpenApiClientDTO openApiClientDTO,
                             @RequestParam(value = "sn") String sn) {
        logger.info("开放平台获取塔机列表,sn:{}", sn);
        OpenApiFseCraneDataDTO data = fseCraneService.deviceInfo(sn);
        return ResultUtil.success(data);
    }

    @GetMapping("/data/real")
    public Result dataReal(@RequestAttribute(OpenApiConst.OPENAPI_CLIENT) OpenApiClientDTO openApiClientDTO,
                           @RequestParam(value = "sn") String sn,
                           @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @RequestParam(value = "startTime") Date startTime,
                           @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @RequestParam(value = "endTime") Date endTime) {
        logger.info("开放平台获取塔机历史数据,sn:{}，startTime：{}，endTime：{}", sn, startTime, endTime);
        ListData<?> data = fseCraneService.dataReal(sn, startTime, endTime);
        return ResultUtil.success(data);
    }


    @GetMapping("/data/record")
    public Result dataRecord(@RequestAttribute(OpenApiConst.OPENAPI_CLIENT) OpenApiClientDTO openApiClientDTO,
                             @RequestParam(value = "sn") String sn,
                             @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @RequestParam(value = "startTime") Date startTime,
                             @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @RequestParam(value = "endTime") Date endTime) {
        logger.info("开放平台获取塔机吊装记录,sn:{}，startTime：{}，endTime：{}", sn, startTime, endTime);
        ListData<OpenApiFseCraneRecordDTO> data = fseCraneService.dataRecord(sn, startTime, endTime);
        return ResultUtil.success(data);
    }

    @PostMapping("/data/add")
    public Result dataAdd(@RequestAttribute(OpenApiConst.OPENAPI_CLIENT) OpenApiClientDTO openApiClientDTO, @RequestBody @Validated FseCraneDataAddParam param) {
        logger.info("开放平台新增塔机实时数据,param：{}", param);
        fseCraneService.dataAdd(param);
        return ResultUtil.success();
    }

    /**
     * 九磊智能塔吊工况数据
     * dataType 0C：实时数据 20：报警信息 3D：循环上报
     * @param data
     * @return
     */
    @PostMapping("anon/jlzn/receiveData")
    public Result jlznReceiveData(@RequestBody String data) {
        logger.info("九磊智能塔吊数据,param：{}", data);
        fseCraneService.jlznReceiveData(data);
        return ResultUtil.success();
    }

    /**
     * 探越塔吊实时数据
     * @param data
     * @return
     */
    @PostMapping("anon/tanyue/realData")
    public Result tanyueRealData(@RequestBody String data) {
        logger.info("探越塔吊实时数据,param：{}", data);
        fseCraneService.tanyueRealData(data);
        return ResultUtil.success();
    }

    /**
     * 探越塔吊报警数据
     * @param data
     * @return
     */
    @PostMapping("anon/tanyue/warnData")
    public Result tanyueWarnData(@RequestBody String data) {
        logger.info("探越塔吊报警数据,param：{}", data);
        fseCraneService.tanyueWarnData(data);
        return ResultUtil.success();
    }

    /**
     * 探越塔吊循环工作数据
     * @param data
     * @return
     */
    @PostMapping("anon/tanyue/workData")
    public Result tanyueWorkData(@RequestBody String data) {
        logger.info("探越塔吊循环工作数据,param：{}", data);
        fseCraneService.tanyueWorkData(data);
        return ResultUtil.success();
    }

}
